<template>
    <el-button type="primary" @click="router.push({ name: 'SaleContractAdd' })">新增</el-button>
    <el-button type="success" @click="UpdProduct()">修改</el-button>
    <el-button @click="DelData">删除</el-button>
    <el-form :inline="true" :model="page" class="demo-form-inline">
        <el-form-item label="合同主题">
            <el-input v-model="page.ContractTopic" clearable />
        </el-form-item>
        <el-form-item label="关联项目">
            <el-input v-model="page.RelevancyIName" clearable />
        </el-form-item>
        <el-form-item label="客户名称">
            <el-input v-model="page.ClientName" clearable />
        </el-form-item>
        <el-form-item label="签约日期">
            <el-date-picker v-model="page.STime" type="datetime" placeholder="Pick a Date" format="YYYY/MM/DD hh:mm:ss"
                value-format="YYYY-MM-DD h:m:s a" />- <el-date-picker v-model="page.ETime" type="datetime"
                placeholder="Pick a Date" format="YYYY/MM/DD hh:mm:ss" value-format="YYYY-MM-DD h:m:s a" />

        </el-form-item>
        <el-form-item label="销售人员">
            <el-input v-model="page.SCSalespeople" clearable />
        </el-form-item>
        <el-form-item label="合同编号">
            <el-input v-model="page.SCcontractNumber" clearable />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="Show">查询</el-button>
        </el-form-item>
    </el-form>


    <el-table :data="showGet" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="序号" width="100" type="index" />
        <el-table-column prop="sCcontractNumber" label="合同编号" width="180" />
        <el-table-column prop="contractTopic" label="合同主题" width="180" />
        <el-table-column prop="relevancyIName" label="关联项目" width="180" />
        <el-table-column prop="clientName" label="客户名称" width="180" />
        <el-table-column prop="scAgencyDate" label="签约日期" width="180" />
        <el-table-column prop="scNum" label="数量合计" />
        <el-table-column prop="scSalespeople" label="销售人员" width="180" />
        <el-table-column prop="scTax" label="税额合计" width="180" />
        <el-table-column prop="sCcontractPrice" label="合同总额" width="180" />
        <el-table-column prop="scPreferentialPrice" label="优惠额度" width="180" />
        <el-table-column prop="preferentialPrice" label="优惠后总额" width="180" />
    </el-table>
    <table style="margin: auto;">
        <tr>
            <td>
                <el-pagination v-model:current-page="page.PageIndex" v-model:page-size="page.PageSize"
                    :page-sizes="[2, 4, 6, 8]" :background="true" layout="total,slot,sizes" :total="page.totalCount">
                    <span> , 每页 {{ page.PageSize }} 条，当前 {{ page.PageIndex }} / {{ page.pageCount }} 页</span>
                </el-pagination>
            </td>
            <td>
                <el-pagination v-model:current-page="page.PageIndex" v-model:page-size="page.PageSize"
                    :page-sizes="[2, 4, 6, 8]" :background="true" prev-text="上一页" next-text="下一页"
                    layout="slot, prev, pager, next" :total="page.totalCount">
                    <el-button :disabled="page.PageIndex == 1" @click="page.PageIndex = 1">首页</el-button>
                </el-pagination>
            </td>
            <td>
                <el-pagination v-model:current-page="page.PageIndex" v-model:page-size="page.PageSize"
                    :page-sizes="[2, 4, 6, 8]" :background="true" prev-text="上一页" next-text="下一页" layout="slot, jumper"
                    :total="page.totalCount">
                    <el-button :disabled="page.PageIndex == page.pageCount"
                        @click="page.PageIndex = page.pageCount">尾页</el-button>
                </el-pagination>
            </td>
        </tr>
    </table>



</template>
<script setup lang="ts">
import http from '../../../http.js'
import { ref, onMounted, reactive } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const ruleFormRef = ref<FormInstance>();

onMounted(() => {
    Show()
})
const page = reactive({
    PageIndex: 1,
    PageSize: 6,
    totalCount: 0,
    pageCount: 0,
    ContractTopic: '',
    RelevancyIName: '',
    ClientName: '',
    STime: '',
    ETime: '',
    SCSalespeople: '',
    SCcontractNumber: ''
})
var showGet = ref([])
const Show = () => {
    axios.get(http.zblnb + '/cw/api/SaleContract/ShowStateSalesReturn', {
        params: {
            PageIndex: page.PageIndex,
            PageSize: page.PageSize,
            ContractTopic: page.ContractTopic,
            RelevancyIName: page.RelevancyIName,
            ClientName: page.ClientName,
            STime: page.STime,
            ETime: page.ETime,
            SCSalespeople: page.SCSalespeople,
            SCcontractNumber: page.SCcontractNumber
        }
    }).then((res) => {
        if (res.data.code == 200) {
            console.log(res.data.data)
            showGet.value = res.data.data.list
            page.totalCount = res.data.data.lcount
            page.pageCount = res.data.data.pcount
        }
    })
}
interface Vendor {
    vendorId: number; // 或者 string  
}
const Del = reactive({
    saleContractId: [],
    delName: ''

})
//多选框赋值
const check1 = ref([])
const handleSelectionChange = (val: Vendor[]) => {
    check1.value = val
    console.log(check1.value)
    Del.saleContractId = []; // 如果希望每次重置数组，请取消注释  
    val.forEach((item) => {
        if (!Del.saleContractId.includes(item.saleContractId)) {
            Del.saleContractId.push(item.saleContractId);
        }
    });
    console.log(Del.saleContractId);

};

const DelData = () => {

    axios.post(http.zblnb + "/cw/api/SaleContract/DelStateSalesReturn", {
        saleContractId: Del.saleContractId,
        delName: Del.delName
    }).then(res => {
        if (res.data.code == 200) {
            ElMessage({
                message: '删除成功',
                type: 'success',

            })
            Show();
        }
    })
}




//修改
const UpdProducts = ref({})
const UpdProduct = () => {
    if (check1.value.length == 0) {
        ElMessage.error('请选择要修改的数据');
        return
    }
    if (check1.value.length > 1) {
        ElMessage.error('要修改的数据只能选择一条');
        return
    }
    UpdProducts.value = check1.value[0]
    console.log(UpdProducts.value)
    router.push("SaleContractUpd?id=" + UpdProducts.value.saleContractId)
}










</script>